﻿@using BAFactory.Fx.Security.Areas.Membership.Extensions;
@model BAFactory.Fx.Security.Areas.Membership.Models.AssignPermissionModel
@{
    ViewBag.Title = "Asignar Permiso";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    function UpdateModules(value) {
        $.ajax({
            url: 'http://@Request.Url.DnsSafeHost:@Request.Url.Port/Membership/Module/ListByArea/' + value,
            success: function (data) {
                $('#@FormsFieldsNames.ModuleId >option').remove();

                $.each(data, function (id, val) {

                    $('#@FormsFieldsNames.ModuleId').append(
                        $('<option></option>').val(val.Item1).html(val.Item2)
                    );
                });

                UpdateActions($('#@FormsFieldsNames.ModuleId').val());


            },
            error: function (data) { alert('error: ' + data); }
        });
    }

    function UpdateActions(value) {
        $.ajax({
            url: 'http://@Request.Url.DnsSafeHost:@Request.Url.Port/Membership/Action/ListByModule/' + value,
            success: function (data) {
                $('#@FormsFieldsNames.ActionId >option').remove();

                $.each(data, function (id, val) {

                    $('#@FormsFieldsNames.ActionId').append(
                        $('<option></option>').val(val.Item1).html(val.Item2)
                    );
                });

                MarkAlreadyAssigned();

            },
            error: function (data) { alert('error: ' + data); }
        });
    }

    function MarkAlreadyAssigned() {
        $.ajax({
            url: 'http://@Request.Url.DnsSafeHost:@Request.Url.Port/Membership/Action/ListByModuleAndUsername?idmodule=' + $('#@FormsFieldsNames.ModuleId').val() + '&username=' + $('#USERNAME').val(),
            success: function (data) {

                $.each(data, function (idx) {
                    var grantedId = data[idx];
                    $.each($('#@FormsFieldsNames.ActionId')[0].options, function (optionIdx) {
                        var optionValue = $('#@FormsFieldsNames.ActionId')[0].options[optionIdx].value;
                        if (optionValue == grantedId)
                            $('#@FormsFieldsNames.ActionId')[0].options[optionIdx].selected = true;
                    })
                })
            },
            error: function (data) { alert('error: ' + data); }
        });
    }

</script>
@Html.Partial("~/Areas/Membership/Views/Controls/ContentTitle.cshtml")
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Permiso</legend>
        <table style="width: 100%;">
            <col style="width: 50%" />
            <col style="width: 50%" />
            <tr>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.UsersOrganization.User.UserName)
                    </div>
                    <div class="editor-field">
                        @Html.Partial("~/Areas/Membership/Views/Controls/SystemUsersTextBox.cshtml")
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Action.Module.Area)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.Action.Module.Area.Id, ViewData[ViewDataKeys.SystemAreas] as SelectList, new { onchange = "UpdateModules(this.value);", style = "width: 99%;" })
                        @Html.ValidationMessageFor(model => model.Action.Module.IdArea)
                    </div>
                </td>
            </tr>
            <tr>
                <td />
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Action.Module)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.IdModule, ViewData[ViewDataKeys.SystemModules] as SelectList, new { onchange = "UpdateActions(this.value);", style = "width: 99%;" })
                        @Html.ValidationMessageFor(model => model.IdModule)
                    </div>
                </td>
            </tr>
            <tr>
                <td />
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Action)
                    </div>
                    <div class="editor-field">
                        @Html.ListBoxFor(model => model.IdAction, ViewData[ViewDataKeys.SystemActions] as SelectList, new { style = "width: 100%;" })
                        @Html.ValidationMessageFor(model => model.IdAction)
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div style="text-align: right;">
                        <input type="submit" value="Asignar" />
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>
}
<br />
@Html.Partial("~/Areas/Membership/Views/Controls/BackToIndexActionLink.cshtml")
<script type="text/javascript" language="javascript">    UpdateModules($("#Action_Module_Area_Id").val()); </script>
